package com.zl.tby.controller;

import com.zl.tby.core.common.R;
import com.zl.tby.core.common.User;
import com.zl.tby.model.Demo;
import com.zl.tby.screen.service.ITbyMountainsScreenService;
import com.zl.tby.screen.vo.mountains.TbyPatrolStatisticBean;
import com.zl.tby.service.DemoService;
import com.zl.tby.vo.DemoForm;
import org.noear.solon.annotation.*;
import org.noear.solon.core.handle.MethodType;
import org.noear.solon.validation.annotation.NotNull;
import org.noear.solon.validation.annotation.Valid;
import org.noear.solon.validation.annotation.Validated;
import org.noear.wood.IPage;

import java.util.Map;

/**
 * @author wangjl
 * @title: DemoController
 * @projectName share
 * @description: 演示接口
 * @date 2024/5/916:08
 */
@Valid
@Controller
@Mapping("/demo")
public class DemoController {

    @Inject
    private DemoService demoService;
    @Inject
    private ITbyMountainsScreenService tbyMountainsScreenService;

    @Mapping(method = MethodType.GET)
    public R<?> page(@Body Map<String, Object> param) {
        return R.ok();
    }


    /**
     * @author wangjl
     * @description: 创建demo
     * @param form 表单对象
     * @return 消息体
     */
    @Mapping(method = MethodType.POST)
    public R<Void> create(@Validated DemoForm form) {
        User user = User.current();
        Demo demo = new Demo();
        demo.setTitle(form.getTitle());
        demo.setContent(form.getContent());
        demo.setCreateBy(user.getUserId());
        demo.setUpdateBy(user.getUserId());
        demoService.create(demo);
        return R.ok();
    }


    /**
     * @author wangjl
     * @description: 创建demo
     * @param id demo id
     * @return 消息体
     */
    @Mapping(method = MethodType.DELETE)
    public R<Void> delete(@NotNull Long id) {
        return demoService.delete(id) == 1 ? R.ok() : R.failed("删除失败");
    }

    @Mapping(method = MethodType.GET)
    public R<IPage<Demo>> page(@NotNull int start, int size, String title) {
        return R.ok(demoService.page(start, size, title));
    }

    @Mapping(value = "info", method = MethodType.GET)
    public R<TbyPatrolStatisticBean> demo() {
        String time = "2024-10-26";
        return R.ok(this.tbyMountainsScreenService.patrolStatistic(time, time, null));
    }

}
